package com.jyu.apts.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.jyu.apts.dao.ItemProcessRecordInfoRepository;
import com.jyu.apts.entity.ItemProcessRecordInfo;
import com.jyu.apts.service.ItemProcessRecordInfoService;

@Service
public class ItemProcessRecordInfoServiceImpl implements ItemProcessRecordInfoService {
	
	@Autowired
	ItemProcessRecordInfoRepository itemProcessRecordInfoDao;
	
	@Override
	public ItemProcessRecordInfo getOne(long id) {
		return itemProcessRecordInfoDao.findById(id).get();
	}

	@Override
	public Page<ItemProcessRecordInfo> getItemProcessRecordInfoPage(Integer pageNumber, Integer pageSize,String search,Long organizationId) {
		if(pageNumber == null || pageNumber <= 0) {
			pageNumber = 1;
		}
		if(pageSize == null || pageSize <= 0) {
			pageSize = 10;
		}
		List<Sort.Order> orders = new ArrayList<Sort.Order>();
		orders.add(new Sort.Order(Sort.Direction.ASC,"id"));
		Sort sort = Sort.by(orders);
		PageRequest pageRequest = new PageRequest(pageNumber-1,pageSize,sort);
		Specification<ItemProcessRecordInfo> spec = new Specification<ItemProcessRecordInfo>() {
			@Override
			public Predicate toPredicate(Root<ItemProcessRecordInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate pre = criteriaBuilder.conjunction();
				if(organizationId!=null) {
					pre = criteriaBuilder.equal(root.get("itemProcessRecord").get("itemBatchRecord").get("itemProductionLine").get("item").get("organization").get("id"), organizationId.longValue());
				}
				if(search!=null && search.trim().length()!=0) {
					Predicate or_pre = criteriaBuilder.disjunction(); 
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("title"), "%"+search+"%"));
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("description"), "%"+search+"%"));
					pre = criteriaBuilder.and(pre,or_pre);
				}
				return pre;
			}
			
		};
		return itemProcessRecordInfoDao.findAll(spec, pageRequest);
	}

	@Override
	public void save(ItemProcessRecordInfo itemProcessRecordInfo) {
		itemProcessRecordInfoDao.saveAndFlush(itemProcessRecordInfo);
	}

	@Override
	public void save(List<ItemProcessRecordInfo> itemProcessRecordInfos) {
		itemProcessRecordInfoDao.saveAll(itemProcessRecordInfos);
		itemProcessRecordInfoDao.flush();
	}

	@Override
	public void delete(List<ItemProcessRecordInfo> itemProcessRecordInfos) {
		itemProcessRecordInfoDao.deleteAll(itemProcessRecordInfos);
	}

	@Override
	public void delete(ItemProcessRecordInfo itemProcessRecordInfo) {
		itemProcessRecordInfoDao.delete(itemProcessRecordInfo);
	}

	@Override
	public List<ItemProcessRecordInfo> findAll() {
		return itemProcessRecordInfoDao.findAll();
	}

	@Override
	public List<ItemProcessRecordInfo> findAllById(List<Long> ids) {
		return itemProcessRecordInfoDao.findAllById(ids);
	}

}
